//
//  ImmediateEvaluationViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/21.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "OrderInformationViewController.h"
#import "OrderInformationViewCell.h"
#import "BookingInformationViewController.h"
#define surePayBtnH 50

@interface OrderInformationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView * tableView;
@property(nonatomic, strong)UIView * headerView;
@property(nonatomic, strong)NSMutableArray *dataArray;//cell标题
@property(nonatomic, strong)NSMutableArray *contentArray;//cell内容
@property(nonatomic, strong)NSDictionary * dic;
@property(nonatomic, strong)NSString * type;//1、旅游  2、活动
@property(nonatomic, strong)NSString * totalMoneyStr;//总金额
@property(nonatomic, strong)NSString * depositStr;//预定金
- (void)surePayBtnClick;
@end

@implementation OrderInformationViewController(networking)
- (void)getOrderListDetail{
    NSDictionary * dic = @{
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token,
                           @"id":@(self.orderId)
                           };
    WS(ws)
    [[HUDHelper sharedInstance]syncLoading:@"翻箱中..."];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/member/orderDetail") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [[HUDHelper sharedInstance]syncStopLoading];
        if (error)
        {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000)
            {
                self.dic = obj[@"data"];
                [self.tableView reloadData];
            }
            else
            {
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];
}
@end

@implementation OrderInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"预订单信息";
    [self.view addSubview:self.tableView];
    
    [self getOrderListDetail];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
        _tableView.backgroundColor = getColor(bgColor);
        _tableView.tableHeaderView = self.headerView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _headerView.backgroundColor = getColor(bgColor);
    }return _headerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@{@"title":@"项目信息:"},@{@"title":@"总金额:"},@{@"title":@"预定金:"},@{@"title":@"添加时间:"}].mutableCopy;
    }return _dataArray;
}

- (void)setDic:(NSDictionary *)dic{
    _dic = dic;
    NSString * projectStr = dic[@"message"];
    NSString * totalMoneyStr = dic[@"price"];
    NSString * depositStr = dic[@"deposit"];
    NSString * addTimeStr = dic[@"addtime"];
    NSString * stateStr = dic[@"state"];
    self.contentArray = [[NSMutableArray alloc] initWithObjects:projectStr,totalMoneyStr,depositStr,addTimeStr,nil];
    
    if ([stateStr isEqualToString:@"待完成"]) {
        UIButton * goPayBtn = [[UIButton alloc] init];
        goPayBtn.frame = CGRectMake(0, SCREEN_HEIGHT - surePayBtnH - 64, SCREEN_WIDTH, surePayBtnH);
        [goPayBtn setBackgroundColor:getColor(@"ffbb02")];
        [goPayBtn setTitle:@"去支付" forState:UIControlStateNormal];
        [goPayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        goPayBtn.titleLabel.font = DEF_FontSize_16;
        [goPayBtn addTarget:self action:@selector(surePayBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:goPayBtn];
    }
}

- (NSMutableArray *)contentArray{
    if (!_contentArray) {
        _contentArray = @[].mutableCopy;
    }return _contentArray;
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellID = @"cell";
    [self.tableView registerClass:[OrderInformationViewCell class] forCellReuseIdentifier:cellID];
    OrderInformationViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    }
    cell.model = self.dataArray[indexPath.row];
    if (self.contentArray.count != 0) {
        cell.contentModel = self.contentArray[indexPath.row];
    }
    return cell;
}

- (void)surePayBtnClick{
    BookingInformationViewController * vc = [[BookingInformationViewController alloc] init];
    vc.type = self.type;
    vc.activityId = [NSString stringWithFormat:@"%i", self.orderId];
    vc.reservationDeposit = self.depositStr;
    vc.totalMoney = self.totalMoneyStr;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
